package com.jjlin.chapter_2;

import java.util.*;
public class CD48{
    public static class Node{
        public int val;
        public Node next;
        public Node(int data){
            this.val = data;
        }
    }

    public static Node createNodeList(int[] nums){
        Node head = new Node(nums[0]);
        Node tmp = head;
        for(int i = 1; i < nums.length; i++){
            Node newNode = new Node(nums[i]);
            tmp.next = newNode;
            tmp = newNode;
        }
        return head;
    }

    //打印两个升序链表的公共部分
    public static void printCommonPart(Node list1, Node list2){
        Node node1 = list1, node2 = list2;
        while(node1 != null && node2 != null){
            if(node1.val < node2.val)
                node1 = node1.next;
            else if(node1.val > node2.val)
                node2 = node2.next;
            else{
                System.out.print(node1.val + " ");
                node1 = node1.next;
                node2 = node2.next;
            }
        }
        System.out.println();
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();
        int[] nums = new int[len];
        for(int i = 0; i < len; i++)
            nums[i] = sc.nextInt();
        Node list1 = createNodeList(nums);
        len = sc.nextInt();
        nums = new int[len];
        for(int i = 0; i < len; i++)
            nums[i] = sc.nextInt();
        Node list2 = createNodeList(nums);
        printCommonPart(list1, list2);
    }
}
